package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaItemModel;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class XunChaItemAdapter extends BaseRecyclerAdapter<XunChaItemModel> {

    private Context context;

    public XunChaItemAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaItemModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_item_content;
        private ImageView iv_item_img;
        private TextView tv_title;
        private ImageView iv_add;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.iv_item_img = itemView.findViewById(R.id.iv_item_img);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.iv_add = itemView.findViewById(R.id.iv_add);
    }

    private void setItemData(MyViewHolder viewHolder, XunChaItemModel data, int postion) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (postion == 0){
            layoutParams.setMargins(0, Utils.dip2px(context, 15), 0 ,Utils.dip2px(context, 15));
        }else {
            layoutParams.setMargins(0, 0, 0 ,Utils.dip2px(context, 15));
        }
        viewHolder.iv_item_img.setVisibility(View.VISIBLE);
        if (StringUtils.isEmpty(data.getLogoUri())){
            viewHolder.iv_item_img.setImageResource(R.mipmap.icon_fire);
        }else{
            GildeUtils.loadImage(context, viewHolder.iv_item_img, data.getLogoUri());
        }
        viewHolder.tv_title.setText(data.getContent());
    }
}
